import os
import cv2
from pathlib import Path
import shutil

UNZIP_FILES = False
MV_AUDIO_FILES = False
MV_VIDEO_FILES = False
EXTRACT_AUDIO = True


if __name__ == '__main__':

    path_root = Path("/mnt/data/DATA/GRID")

    if UNZIP_FILES:
        path_zip = path_root.joinpath("zip_data")
        path_data = path_root.joinpath("unzip_data")
        v_zip_files = sorted(path_zip.glob("./*.tar"))

        print(f"Have {len(v_zip_files)} zip files, \n {v_zip_files[:10]}")

        
        for i, izip in enumerate(v_zip_files):
            
            ipath_data = path_data.joinpath(str(izip.stem))

            # if not ipath_data.exists():
            cmd = f"cd {str(path_zip)}; tar -xf {str(izip)} -C {str(path_data)}"
            print(f"RUN: {cmd}")
            os.system(cmd)

            #break

    if MV_AUDIO_FILES:
        path_unzip = path_root.joinpath("unzip_data")
        v_ids_file = sorted(path_unzip.glob("*"))
        
        print(f"Have {len(v_ids_file)} persons files, \n {v_ids_file[:10]}")

        for i, iid in enumerate(v_ids_file):
            
            if "align" in str(iid):
                continue

            cmd = f"cd {str(iid)}; mkdir audio;  mv *.wav audio"
            print(f"RUN: {cmd}")
            os.system(cmd)

            #break

    if MV_VIDEO_FILES:
        path_data = path_root.joinpath("unzip_data")
        v_ids = sorted(path_data.glob("*"))

        print(f"Have {len(v_ids)} dir files, \n {v_ids[:10]}")

        # for each person
        for i, ipath_id in enumerate(v_ids):
            id_name = ipath_id.stem

            if "align" in str(ipath_id):
                continue

            dir_video = path_data.joinpath(id_name, "video/mpg_6000")
            v_dir_videos = sorted(dir_video.glob("*.mpg"))

            print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:10]}")
            
            # for each video
            for ipath_videos in v_dir_videos:
                ivideo_name = ipath_videos.stem

                # copy video
                ipath_video_target = path_root.joinpath("processing", id_name, ivideo_name, "video/video_orignal.mp4")
                
                if not ipath_video_target.exists():
                    ipath_video_target.parent.mkdir(parents=True, exist_ok=True)
                    shutil.copy(src=str(ipath_videos), dst=str(ipath_video_target))
                    #print(f"copy {ipath_videos} --> {ipath_video_target}")

                # # copy audio 
                # ipath_audio_source = path_data.joinpath(id_name, f"audio/{ivideo_name}.wav")
                # ipath_audio_target = path_root.joinpath("processing", id_name, ivideo_name, "audio/audio_orignal.wav")

                # if not ipath_audio_target.exists():
                #     ipath_audio_target.parent.mkdir(parents=True, exist_ok=True)
                #     shutil.copy(src=str(ipath_audio_source), dst=str(ipath_audio_target))
                #     #print(f"copy {ipath_audio_source} --> {ipath_audio_target}")

            # break
  
    if EXTRACT_AUDIO:
        
        path_data = path_root.joinpath("unzip_data")
        v_ids = sorted(path_data.glob("*"))

        print(f"Have {len(v_ids)} dir files, \n {v_ids[:10]}")

        # for each person
        for i, ipath_id in enumerate(v_ids):
            id_name = ipath_id.stem

            if "align" in str(ipath_id):
                continue

            dir_video = path_data.joinpath(id_name, "video/mpg_6000")
            v_dir_videos = sorted(dir_video.glob("*.mpg"))

            print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:10]}")
            
            # for each video
            for ipath_videos in v_dir_videos:
                ivideo_name = ipath_videos.stem

                # copy video
                ipath_video_source = path_root.joinpath("processing", id_name, ivideo_name, "video/video_orignal.mp4")
                ipath_audio_target = path_root.joinpath("processing", id_name, ivideo_name, "audio/audio_orignal.wav")
                ipath_audio_target.parent.mkdir(parents=True, exist_ok=True)
                # extract audio
                cmd = f'ffmpeg -y -i {str(ipath_video_source)} -vn -acodec pcm_s16le -ac 1 -ar 16000 {str(ipath_audio_target)}'
                #print(f"RUN: {cmd}")
                os.system(cmd)

                # break
            break




                




